//
//  LYHttpManager.m
//  LYGameSDK
//
//  Created by A on 2021/4/13.
//
#import "AppDelegate.h"
#import "HttpManager.h"
#import <AFNetworking/AFNetworking.h>
#import <CommonCrypto/CommonDigest.h>

typedef enum : NSUInteger {
    POST,
    GET,
    PUT,
    DELETE
} HTTPTYPE;

@interface HttpManager ()
@property (nonatomic, assign) HTTPTYPE type;
@property (nonatomic, strong) NSString* url;
@property (nonatomic, strong) NSDictionary * params;
@property (nonatomic, copy) void (^successBlock)(NSDictionary * dic);
@property (nonatomic, copy) void (^errorBlock)(NSError * error) ;
@end


@implementation HttpManager


+ (HttpManager *)shareInstance{
    static HttpManager * instace = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        instace = [[HttpManager alloc] init];
    });
    return instace;
}

- (id)init {
    if (self = [super init]) {
        baseURLStr = [NSString stringWithFormat:@"%@",@""];
    }
    return self;
}

- (void)getNewVersonSuccess:(void (^)(NSDictionary * _Nonnull))successBlock error:(void (^)(NSError * _Nonnull))errorBlock{
    

    //苹果商店检测
//    AFHTTPSessionManager*manager = [AFHTTPSessionManager manager];
//    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//    manager.responseSerializer.acceptableContentTypes= [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json",@"text/javascript",nil];
//    NSString * urlStr = @"https://itunes.apple.com/cn/lookup?id=1573880476";
//    [manager  POST:urlStr parameters:@{@"id":@"1573880476"} headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSArray*array = responseObject[@"results"];
//        if (array.count) {
//            NSDictionary * dic = array[0];
//            NSString * appStoreVersion = dic[@"version"];
//            //打印版本号
//            NSDictionary *infoDic = [[NSBundle mainBundle]infoDictionary];
//            NSString *localVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
//            NSComparisonResult  result =  [localVersion compare:appStoreVersion options:NSNumericSearch];
//            if (result == NSOrderedAscending) {
//                successBlock([dic valueForKey:@"releaseNotes"]);
//            }
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
//    }];
    
}


/** 基础请求接口*/
- (void)httpMethod:(HTTPTYPE)method  URL:(NSString *)urlStr Paraments:(NSDictionary *)params success:(void(^)(NSDictionary *dict))successBlock error:(void(^)(NSError * _Nonnull error))errorBlock {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/javascript",@"text/plain", nil];
    
  
    NSString * url = [NSString stringWithFormat:@"%@%@",baseURLStr,urlStr];
    NSLog(@"HTTPManage 发起请求:%@ \n请求链接：%@", params, url);
    if (method == POST) {
        [manager POST:url parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dict = responseObject;
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([[dict valueForKey:@"code"] intValue] == 200) {
                    successBlock(dict);
                }else{
                   
                    successBlock(nil);
                }
            });
            NSLog(@"HTTPManage 发起请求:%@ \n请求链接：%@ \n 返回数据：%@", params, url, responseObject);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                
                errorBlock(error);
            });
        }];
        
    }else if(method == GET){
        [manager GET:url parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dict = responseObject;
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([[dict valueForKey:@"code"] intValue] == 200) {
                    successBlock(dict);
                }else{
                  
                    successBlock(nil);
                }
                
            });
            NSLog(@"HTTPManage 发起请求:%@ \n请求链接：%@ \n 返回数据：%@", params, url, responseObject);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            dispatch_async(dispatch_get_main_queue(), ^{
               
                errorBlock(error);
            });
        }];
    }else if(method == PUT){
        [manager PUT:url parameters:params headers:nil  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dict = responseObject;
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([[dict valueForKey:@"code"] intValue] == 200) {
                    successBlock(dict);
                }else{
                  
                    successBlock(nil);
                }
                
            });
            NSLog(@"HTTPManage 发起请求:%@ \n请求链接：%@ \n 返回数据：%@", params, url, responseObject);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            dispatch_async(dispatch_get_main_queue(), ^{
//                [SVProgressHUD showErrorWithStatus:@"网络错误"];
                errorBlock(error);
            });
        }];
    }else if(method == DELETE){
        [manager DELETE:url parameters:params headers:nil  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dict = responseObject;
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([[dict valueForKey:@"code"] intValue] == 200) {
                    successBlock(dict);
                }else{
                    successBlock(nil);
                }
                
            });
            NSLog(@"HTTPManage 发起请求:%@ \n请求链接：%@ \n 返回数据：%@", params, url, responseObject);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            dispatch_async(dispatch_get_main_queue(), ^{
//                [SVProgressHUD showErrorWithStatus:@"网络错误"];
                errorBlock(error);
            });
        }];
    }
    
}

@end

